package com.demo.demo1;

import com.demo.demo1.beans.CarVO;
import com.demo.demo1.beans.CityCarVO;
import com.demo.demo1.beans.DateCityCarVO;
import com.demo.util.FileUtil;
import com.demo.util.excel.beans.ExcelColumnVO;
import com.demo.util.excel.builders.ExcelColumnsBuilder;
import com.demo.util.excel.builders.ExcelStyleBuilder;
import com.demo.util.excel.builders.ExportExcelBuilder;
import lombok.Cleanup;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Demo1 {

    public static void main(String[] args) throws IOException {

        ExcelStyleBuilder excelStyle = new ExcelStyleBuilder()
                .add("headerStyle", Demo1::getHeaderStyle)
                .add("subHeaderStyle", Demo1::getSubHeaderStyle)
                .add("cellStyle", Demo1::getCellStyle)
                .add("subCellStyle", Demo1::getSubCellStyle)
        ;

        List<DateCityCarVO> exportData = getExportData();

        List<ExcelColumnVO<DateCityCarVO>> columns = new ExcelColumnsBuilder<DateCityCarVO>()
                .add(DateCityCarVO::getDate)
                .add("杭州", "subHeaderStyle").setChildren(new ExcelColumnsBuilder<DateCityCarVO>()
                        .add("投放车辆数", "cityCars.0.carInfo.carCount")
                        .add("在租车辆数", "cityCars.0.carInfo.useCarCount")
                        .add("租赁率", "cityCars.0.carInfo.useCarRate")
                        .add("当日首期新增租赁数", "cityCars.0.carInfo.addUseCarInDay")
                        .add("当日续租新增租赁数", "cityCars.0.carInfo.useCarCount")
                        .add("退租数", "cityCars.0.carInfo.unusedCarInDay")
                )
                .add("上海", "subHeaderStyle").setChildren(new ExcelColumnsBuilder<DateCityCarVO>()
                        .add("投放车辆数", "cityCars.1.carInfo.carCount")
                        .add("在租车辆数", "cityCars.1.carInfo.useCarCount")
                        .add("租赁率", "cityCars.1.carInfo.useCarRate")
                        .add("当日首期新增租赁数", "cityCars.1.carInfo.addUseCarInDay")
                        .add("当日续租新增租赁数", "cityCars.1.carInfo.useCarCount")
                        .add("退租数", "cityCars.1.carInfo.unusedCarInDay")
                )
                .done();

        @Cleanup
        XSSFWorkbook workbook = new XSSFWorkbook();
        new ExportExcelBuilder<DateCityCarVO>(workbook)
                .addStyle(excelStyle)
                .addSheet("Sheet 1", columns, exportData)
                .save("D:\\Downloads\\test.xlsx");

        FileUtil.openFile("D:\\Downloads\\test.xlsx");
    }

    public static XSSFCellStyle getHeaderStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.getIndex());
        font.setBold(true);
        font.setFontHeight(14);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(244,247,253)));
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getSubHeaderStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFFont font = workbook.createFont();
        font.setColor(new XSSFColor(new java.awt.Color(190,190,190)));
        font.setBold(true);
        font.setFontHeight(20);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(244,247,253)));
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getCellStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.getIndex());
        font.setFontHeight(12);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getSubCellStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFFont font = workbook.createFont();
        font.setColor(new XSSFColor(new java.awt.Color(190,190,190)));
        font.setFontHeight(12);
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    private static List<DateCityCarVO> getExportData() {
        List<DateCityCarVO> results = new ArrayList<>();
        results.add(new DateCityCarVO("11月2日",getExportCityCars()));
        results.add(new DateCityCarVO("11月3日",getExportCityCars()));
        return results;
    }

    private static List<CityCarVO> getExportCityCars() {
        List<CityCarVO> list = new ArrayList<>();
        list.add(new CityCarVO("杭州", getExportCarInfo(1)));
        list.add(new CityCarVO("上海", getExportCarInfo(2)));
        return list;
    }

    private static CarVO getExportCarInfo(int num) {
        return new CarVO(
                50 * num,
                5 * num,
                "10.00%",
                150 * num,
                65 * num,
                5 * num
        );
    }

}
